package com.gec.system.controller;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.system.service.SysMovieService;
import com.gec.system.system.SysCategory;
import com.gec.system.system.SysMenu;
import com.gec.system.system.SysMovie;
import com.gec.system.system.SysPlayAuth;
import com.gec.system.util.Result;
import com.gec.system.utils.VodTemplate;
import com.gec.system.vo.SysQueryMovieVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "影視目錄")
@RequestMapping("/admin/system/movie")
public class SysMovieController {

    @Autowired
    private SysMovieService movieService;
    @Autowired
    private VodTemplate vodTemplate;
    @GetMapping("{page}/{limit}")
    public Result findByPage(@PathVariable("page")Long pageIndex , @PathVariable Long limit , SysQueryMovieVo vo){
        Page<SysMovie> page = new Page<>(pageIndex, limit);
        IPage<SysMovie> sysMovieIPage = movieService.selectPage(page, vo);
        return Result.ok(sysMovieIPage);

    }
    @GetMapping("find/{id}")
    public Result findById(@PathVariable Long id){
        SysMovie sysMovie = movieService.getById(id);
        return  Result.ok(sysMovie);
    }
    @GetMapping("getPlayAuth/{id}")
    public Result getPlayAuthById(@PathVariable Long id) throws ClientException {
        SysMovie sysMovie = movieService.getById(id);
        String playId = sysMovie.getPlayId();
        String image = sysMovie.getImage();
        String auth = vodTemplate.getVideoPlayAuthResponse(playId).getPlayAuth();
        SysPlayAuth playAuth = new SysPlayAuth(playId, image,auth);

        return  Result.ok(playAuth);
    }
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        boolean remove = movieService.removeById(id);
        return remove?Result.ok():Result.fail();
    }
    @DeleteMapping("removes")
    public Result removes(@RequestBody List<Long> ids){
        boolean removeByIds = movieService.removeByIds(ids);
        return removeByIds?Result.ok():Result.fail();
    }
    @PostMapping("add")
    public Result add(@RequestBody SysMovie sysMovie) {
        boolean save = movieService.save(sysMovie);
        return Result.ok(save);
    }
    @PostMapping("update")
    public Result update(@RequestBody SysMovie sysMovie){
        boolean update = movieService.updateById(sysMovie);
        return update?Result.ok():Result.fail();
    }
}
